﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllNhomHang(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblNhomHangs.Where(x => x.TenNhomHang.Contains(strTimkiem)).OrderBy(x => x.TenNhomHang);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaNhomHang;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenNhomHang;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblNhomHang GetNhomHangById(string manhomhang)
        {
            return _dal.tblNhomHangs.Where(x => x.MaNhomHang == manhomhang).FirstOrDefault();
        }

        public void InsertNhomHang(tblNhomHang nhomhang)
        {
            _dal.tblNhomHangs.AddObject(nhomhang);
            _dal.SaveChanges();
        }

        public void UpdateNhomHang(string maNhomHangCu, tblNhomHang nhomhang)
        {
            var NhomHangUpdate = _dal.tblNhomHangs.Where(x => x.MaNhomHang == maNhomHangCu).FirstOrDefault();
            NhomHangUpdate.MaNhomHang = nhomhang.MaNhomHang;
            NhomHangUpdate.TenNhomHang = nhomhang.TenNhomHang;
            NhomHangUpdate.GhiChu = nhomhang.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteNhomHang(IEnumerable<string> manhomhangs)
        {
            var listDelete = _dal.tblNhomHangs.Where(x => manhomhangs.Contains(x.MaNhomHang));
            foreach (var tblnhomhang in listDelete)
            {
                _dal.tblNhomHangs.DeleteObject(tblnhomhang);
            }
            _dal.SaveChanges();
        }

        public tblNhomHang BuildNhomHang(string maNH, string tenNH, string ghichu)
        {
            return new tblNhomHang { MaNhomHang = maNH, TenNhomHang = tenNH,GhiChu = ghichu };
        }
    }
}
